package com.ideal.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ideal.config.Constant;
import com.ideal.dto.DictInfoDto;
import com.ideal.dto.ResultDto;
import com.ideal.dto.config.req.FileConfigPageReqDto;
import com.ideal.dto.config.req.SysTreeDictReqDto;
import com.ideal.dto.config.res.FileConfigPageRsqDto;
import com.ideal.dto.config.res.SysTreeDictPageDto;
import com.ideal.enums.GlobalExceptionEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ideal.mapper.SysTreeDictMapper;
import com.ideal.model.SysTreeDict;
import com.ideal.service.ISysTreeDictService;

/**
 * 树形结构字典Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-21
 */
@Service
public class SysTreeDictServiceImpl implements ISysTreeDictService {
    @Autowired
    private SysTreeDictMapper sysTreeDictMapper;

    /**
     * 查询树形结构字典
     *
     * @param dictId 树形结构字典主键
     * @return 树形结构字典
     */
    @Override
    public SysTreeDict selectSysTreeDictByDictId(Long dictId) {
        return sysTreeDictMapper.selectSysTreeDictByDictId(dictId);
    }

    /**
     * 查询树形结构字典列表
     *
     * @param sysTreeDict 树形结构字典
     * @return 树形结构字典
     */
    @Override
    public List<SysTreeDict> selectSysTreeDictList(SysTreeDict sysTreeDict) {
        return sysTreeDictMapper.selectSysTreeDictList(sysTreeDict);
    }

    /**
     * 新增树形结构字典
     *
     * @param sysTreeDict 树形结构字典
     * @return 结果
     */
    @Override
    public int insertSysTreeDict(SysTreeDict sysTreeDict) {
        sysTreeDict.setCreateTime(new Date());
        return sysTreeDictMapper.insertSysTreeDict(sysTreeDict);
    }

    /**
     * 修改树形结构字典
     *
     * @param sysTreeDict 树形结构字典
     * @return 结果
     */
    @Override
    public int updateSysTreeDict(SysTreeDict sysTreeDict) {
        sysTreeDict.setUpdateTime(new Date());
        return sysTreeDictMapper.updateSysTreeDict(sysTreeDict);
    }

    /**
     * 批量删除树形结构字典
     *
     * @param dictId 需要删除的树形结构字典主键
     * @return 结果
     */
    @Override
    public int deleteSysTreeDictByDictIds(Long dictId) {
        return sysTreeDictMapper.deleteSysTreeDictByDictId(dictId);
    }

    /**
     * 删除树形结构字典信息
     *
     * @param dictId 树形结构字典主键
     * @return 结果
     */
    @Override
    public int deleteSysTreeDictByDictId(Long dictId) {
        return sysTreeDictMapper.deleteSysTreeDictByDictId(dictId);
    }

    @Override
    public List<DictInfoDto> dictChildDetail(String dictValue) {
        return sysTreeDictMapper.findChildByDictValue(dictValue);
    }

    @Override
    public PageInfo page(SysTreeDictReqDto sysTreeDictReqDto) {

        SysTreeDict sysTreeDict = new SysTreeDict();
        BeanUtils.copyProperties(sysTreeDictReqDto, sysTreeDict);
        sysTreeDict.setParentId(0L);
        PageHelper.startPage(sysTreeDictReqDto.getPageNum(), sysTreeDictReqDto.getPageSize());
        List<SysTreeDictPageDto> sysTreeDicts = this.sysTreeDictMapper.selectParentSysTreeDictList(sysTreeDict);
        PageInfo<SysTreeDictPageDto> page = new PageInfo<>(sysTreeDicts);

        for (SysTreeDictPageDto treeDict : sysTreeDicts) {
            treeDict.setChildren(getChildDict(treeDict.getDictId(), sysTreeDictReqDto));

        }
        return page;
    }

    @Override
    public DictInfoDto findOneByDictLabel(String dictLabel) {

        return sysTreeDictMapper.findOneByDictLabel(dictLabel);
    }

    @Override
    public DictInfoDto findParentOneByDictValue(String dictValue) {

        return sysTreeDictMapper.findOneByDictValueAndParent(dictValue);
    }

    /**
     * 获取字典子数据
     *
     * @param parentId
     * @param sysTreeDictReqDto
     * @return
     */
    private List<SysTreeDictPageDto> getChildDict(Long parentId, SysTreeDictReqDto sysTreeDictReqDto) {

        List<SysTreeDictPageDto> list = new ArrayList<>();
        final SysTreeDict sysTreeDict = new SysTreeDict();
        sysTreeDict.setParentId(parentId);
        BeanUtils.copyProperties(sysTreeDictReqDto, sysTreeDict);
        List<SysTreeDict> sysTreeDictList = this.sysTreeDictMapper.selectSysTreeDictList(sysTreeDict);
        for (SysTreeDict treeDict : sysTreeDictList) {
            //if (treeDict.getParentId().equals(sysTreeDict.getDictId())) {
            SysTreeDictPageDto sysTreeDictPageDto = new SysTreeDictPageDto();
            BeanUtils.copyProperties(treeDict, sysTreeDictPageDto);
            sysTreeDictPageDto.setChildren(getChildDict(treeDict.getDictId(), sysTreeDictReqDto));
            list.add(sysTreeDictPageDto);
            //}
        }
        return list;
    }

    /**
     * 切换文件配置开关
     *
     * @param dictValue
     * @return
     */
    @Override
    public ResultDto checkFileSwitch(String dictValue) {
        final DictInfoDto dictInfoDto = this.sysTreeDictMapper.findOneByDictValueAndParent(Constant.FILE_CONFIG_SWITCH);
        if (Objects.nonNull(dictInfoDto)) {

            final SysTreeDict sysTreeDict = this.sysTreeDictMapper.findChildOneByChildDictValueAndParentId(dictValue, dictInfoDto.getDictId());
            if (Objects.nonNull(sysTreeDict)) {

                Long status = sysTreeDict.getDictStatus() == 0 ? 1L : 0L;
                sysTreeDict.setDictStatus(status);
                this.sysTreeDictMapper.updateSysTreeDict(sysTreeDict);
                return ResultDto.success(status);
            }
        }
        return ResultDto.fail(GlobalExceptionEnum.UNKNOWN_ERROR);
    }

    @Override
    public ResultDto getFileState(String dictValue) {
        final DictInfoDto dictInfoDto = this.sysTreeDictMapper.findOneByDictValueAndParent(Constant.FILE_CONFIG_SWITCH);
        if (Objects.nonNull(dictInfoDto)) {

            final SysTreeDict sysTreeDict = this.sysTreeDictMapper.findChildOneByChildDictValueAndParentId(dictValue, dictInfoDto.getDictId());
            if (Objects.nonNull(sysTreeDict)) {

                sysTreeDict.setDictStatus(sysTreeDict.getDictStatus());
                return ResultDto.success(sysTreeDict.getDictStatus());
            }
        }
        return ResultDto.fail(GlobalExceptionEnum.UNKNOWN_ERROR);
    }

    @Override
    public SysTreeDict findFileSwitchDataByDictValue(String dictValue) {
        final DictInfoDto dictInfoDto = this.sysTreeDictMapper.findOneByDictValueAndParent(Constant.FILE_CONFIG_SWITCH);
        if (Objects.nonNull(dictInfoDto)) {

            return this.sysTreeDictMapper.findChildOneByChildDictValueAndParentId(dictValue, dictInfoDto.getDictId());
        }
        return null;
    }

}
